package com.qiwx.pigeon.activity.home;

import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.andview.refreshview.XRefreshView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.qiwx.pigeon.R;
import com.qiwx.pigeon.Utils.MyToast;
import com.qiwx.pigeon.app.AppContext;
import com.qiwx.pigeon.base.BaseMVPActivity;
import com.qiwx.pigeon.data.vo.CageBeanVo;
import com.qiwx.pigeon.data.vo.CageRepairBean;
import com.qiwx.pigeon.data.vo.EliminateBean;
import com.qiwx.pigeon.data.vo.PairBean;
import com.qiwx.pigeon.data.vo.WareHouseBean;
import com.qiwx.pigeon.data.vo.pageinfo.PageInfo;
import com.qiwx.pigeon.presenter.CageRemindPresenter;
import com.qiwx.pigeon.viewinterface.ICageRemindView;
import com.qiwx.pigeon.widget.ArrowView;
import com.qiwx.pigeon.widget.itemderection.LineItemDecoration;

import org.angmarch.views.NiceSpinner;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @description
 * @Author qiwx
 * @Date 2020-04-30 09:54
 **/
public abstract class BaseRemindActivity extends BaseMVPActivity<ICageRemindView, CageRemindPresenter> implements ICageRemindView {

    @BindView(R.id.title_content)
    TextView titleContent;
    @BindView(R.id.title_return)
    ArrowView titleReturn;
    @BindView(R.id.title_right)
    TextView titleRight;
    @BindView(R.id.spinner_wh)
    NiceSpinner spinnerWh;
    @BindView(R.id.txt_amount_name)
    TextView txtAmountName;
    @BindView(R.id.txt_amount)
    TextView txtAmount;
    @BindView(R.id.lin_recycleview_headview)
    LinearLayout linRecycleviewHeadview;
    @BindView(R.id.txt_nodata)
    TextView txtNodata;
    @BindView(R.id.lin_nodata)
    LinearLayout linNodata;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.xRefreshView)
    XRefreshView xRefreshView;
    BaseQuickAdapter adapter;

    public int wh_id;
    int pageNo = 1;
    int pageSize = 20;
    boolean isFresh = true;
    int allCount;
    View headView;

    @Override
    public void initAfterOnCreate() {

    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_base_remind;
    }

    @Override
    public CageRemindPresenter createPresenter() {
        return new CageRemindPresenter(new WeakReference(this));
    }

    public void initView() {
        if(addListViewHead()>0){
            headView=LayoutInflater.from(this).inflate(addListViewHead(),null);
            linRecycleviewHeadview.addView(headView);
        }
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recyclerview.setLayoutManager(linearLayoutManager);
//        recyclerview.setBackgroundColor(getResources().getColor(R.color.white));

//        int space = ScreenUtils.dip2px(getActivity(), 1);
        recyclerview.addItemDecoration(new LineItemDecoration(1));
        recyclerview.setAdapter(getAdapter());
        //设置刷新完成以后，headerview固定的时间
//        xRefreshView.setPinnedTime(1000);
//        xRefreshView.setMoveForHorizontal(false);
        //xRefreshView.setPullLoadEnable(true);
        xRefreshView.setHideFooterWhenComplete(true);

//        xRefreshView.setAutoRefresh(false);
//        xRefreshView.setAutoLoadMore(false);
//        xRefreshView.mPullRefreshing=true;
        //设置静默加载时提前加载的item个数
//        xefreshView1.setPreLoadCount(4);

        xRefreshView.setXRefreshViewListener(new XRefreshView.SimpleXRefreshListener() {

            @Override
            public void onRefresh(boolean isPullDown) {
                loadData(true);

            }

            @Override
            public void onLoadMore(boolean isSilence) {
                loadData(false);
            }
        });
        initSpinner();
    }

    public abstract void loadData(boolean isFresh);

    public abstract BaseQuickAdapter getAdapter();

    public abstract int getSize();
    public abstract int addListViewHead();


    public void initSpinner() {
        List<WareHouseBean> list = AppContext.houses;
        List<String> data = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            data.add(list.get(i).getName());
        }

        spinnerWh.attachDataSource(data);
        spinnerWh.setOnSpinnerItemSelectedListener((parent, view, position, id) -> {
            wh_id = list.get(position).getId();
            loadData(true);
        });
        int select = 0;
        if (wh_id <= 0) {
            if (list.size() > 0) {
                wh_id = list.get(0).getId();
                loadData(true);
            }
        } else {
            for (int i = 0; i < list.size(); i++) {
                if (wh_id == list.get(i).getId()) {
                    wh_id = list.get(i).getId();
                    select = i;
                    loadData(true);
                    break;
                }
            }
            spinnerWh.setSelectedIndex(select);

        }
    }


    public void updateFreshState(boolean isHasNextPage) {
        if (isFresh) {
            xRefreshView.stopRefresh();
        } else {
            xRefreshView.stopLoadMore();
        }
        txtAmount.setText(allCount + "");
        if (getSize() == 0) {
            xRefreshView.setVisibility(View.GONE);
            linNodata.setVisibility(View.VISIBLE);
        } else {
            xRefreshView.setVisibility(View.VISIBLE);
            linNodata.setVisibility(View.GONE);
            if (!isFresh&&!isHasNextPage) {
                MyToast.showLong("已经没有更多了");
            }
        }
        if(isHasNextPage){
            xRefreshView.setPullLoadEnable(true);
        }else {
            xRefreshView.setPullLoadEnable(false);

        }
        adapter.notifyDataSetChanged();
    }
    public void updateAmount(int addAmount){
        allCount=allCount+addAmount;
        txtAmount.setText(allCount+"");
    }

    @Override
    public void onQueryCageSuccess(PageInfo<CageBeanVo> bean) {
        if (bean == null)
            return;
        allCount= bean.getTotal();
        updateFreshState(bean.isHasNextPage());

    }

    @Override
    public void onQueryCageRepairSuccess(PageInfo<CageRepairBean> bean) {
        if (bean == null)
            return;
        allCount= bean.getTotal();
        updateFreshState(bean.isHasNextPage());
    }

    @Override
    public void onQueryPairCageSuccess(PageInfo<PairBean> bean) {
        if (bean == null)
            return;
        allCount= bean.getTotal();
        updateFreshState(bean.isHasNextPage());
    }

    @Override
    public void onQueryEliminateBeanSuccess(PageInfo<EliminateBean> bean) {
        if (bean == null)
            return;
        allCount= bean.getTotal();
        updateFreshState(bean.isHasNextPage());
    }

    @Override
    public void onFailure(String error) {
        if (isFresh) {
            xRefreshView.stopRefresh();
        } else {
            xRefreshView.stopLoadMore();
        }

    }


    @OnClick({R.id.title_return, R.id.txt_nodata})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_return:
                finish();
                break;
            case R.id.txt_nodata:
                break;
        }
    }
}
